package com.yixin.ystartlibrary.utils;

import com.google.gson.JsonObject;

import java.math.BigDecimal;

/**
 * User: Ystar
 * Date: 2018/1/28
 * Content:
 */

public class FloatFormatUtils {

    public static String formatFloat(JsonObject object, String key, int count){
        float f = object.get(key).getAsFloat();
        return formatFloat(f, count);
    }

    public static float formatToFloat(JsonObject object, String key, int count){
        float f = object.get(key).getAsFloat();
        return formatToFloat(f, count);
    }

    /**
     * 取小数点,四舍五入后.
     */
    public static String formatDouble(double str, int count) {
        BigDecimal bd = new BigDecimal(str);
        bd = bd.setScale(count, BigDecimal.ROUND_HALF_UP);
        return bd.toString();
    }

    /**
     * 取小数点,四舍五入后.
     */
    public static String formatFloat(float str, int count) {
        BigDecimal bd = new BigDecimal(str);
        bd = bd.setScale(count, BigDecimal.ROUND_HALF_UP);
        return bd.toString();
    }

    /**
     * 取小数点,四舍五入后.
     */
    public static float formatToFloat(float str, int count) {
        BigDecimal bd = new BigDecimal(str);
        bd = bd.setScale(count, BigDecimal.ROUND_HALF_UP);
        return bd.floatValue();
    }

}
